package com.api.syh56.shengyunhui.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.entity.DedicatedLines;
import com.api.syh56.shengyunhui.entity.Routes;
import com.api.syh56.shengyunhui.entity.User;
import com.api.syh56.shengyunhui.entity.dto.DedicatedLinesDto;
import com.api.syh56.shengyunhui.service.DedicatedLinesService;
import com.api.syh56.shengyunhui.service.RoutesService;
import com.api.syh56.shengyunhui.service.UserService;
import com.api.syh56.shengyunhui.util.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专线
 */
@RestController
@RequestMapping("dedicatedLines")
public class DedicatedLinesController {

    @Autowired
    private DedicatedLinesService dService;

    @Autowired
    private RoutesService routesService;

    @Autowired
    private UserService userService;

    @PostMapping("insert")
    public CommonResult<Object> insert(@RequestBody DedicatedLinesDto dedicatedLinesDto){
        System.out.println("controller:"+dedicatedLinesDto);
//        String id = BaseUserInfo.get(UserInfoEnu.id.getCode());
//        dedicatedLines.setUid(Long.valueOf(id));
        dService.insert(dedicatedLinesDto);
        return CommonResult.success(null);
    }

    @PostMapping("deleteBy")
    public CommonResult<Object> delete(@RequestBody DedicatedLines dedicatedLines){
        dService.deleteBy(dedicatedLines);
        return CommonResult.success(null);
    }

    @PostMapping("updateBy")
    public CommonResult<Object> updateBy(@RequestBody DedicatedLinesDto dedicatedLinesDto){
        dService.updateBy(dedicatedLinesDto);
        return CommonResult.success(null);
    }

    @PostMapping("selectAll")
    public CommonResult<Object> selectAll(){
        List<DedicatedLines> dList = dService.selectAll();
        return CommonResult.success(dList);
    }

    /**
     * 查询专线
     * @param routesDTO
     * @param pageSize
     * @param pageNum
     * @return
     */

    @GetMapping(value = "selectByCity")
    public CommonResult<CommonPage<User>> selectByCity(Routes routesDTO,
                                             @RequestParam Integer pageSize,
                                             @RequestParam Integer pageNum){
        System.out.println("CpageSize:"+pageSize);

        Long oriAdCode = routesDTO.getOriAdCode();
        Long dstAdCode = routesDTO.getDstAdCode();

        QueryWrapper<Routes> routesQueryWrapper = new QueryWrapper<>();
        routesQueryWrapper.lambda()
                .eq(Routes::getOriAdCode,oriAdCode)
                .eq(Routes::getDstAdCode,dstAdCode)
                .orderByDesc(Routes::getCreationTime);

        Page<Routes> page = new Page<>(pageNum,pageSize);
        Page<Routes> routesPage = routesService.page(page, routesQueryWrapper);


        Page<User> userPage = routesService.RoutesPageToUserPage(routesPage);


        return CommonResult.success(CommonPage.restPage(userPage));
    }
}
